using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models;
using FiltronASP.Helpers.Translations;
using System.Text;


namespace FiltronASP.Helpers.AjaxModel
{
    public static class FiltersTableHelperWIX
    {
        public static string generateFiltersTable(int engineId, int modelId, string languageName)
        {
            if ((engineId.Equals(0)) || (modelId.Equals(0)))
            {
                return "nulls";
            }
            int airCol = 6;
            if (languageName == "polish")
            {
                airCol = 7;
            }
            IWixFilterEngineDAO wixFilterEngine = new WixFilterEngineDAO();
            IWixEngineDAO wixEngineDAO = new WixEngineDAO();
            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            IWixFilterEngineCommentsDAO wixFilterEngineCommentsDAO = new WixFilterEngineCommentsDAO();
            IWixFilterEngineFootnoteDAO wixFilterEngineFootnoteDAO = new WixFilterEngineFootnoteDAO();
            IWixFootnoteDAO wixFootnoteDAO = new WixFootnoteDAO();

            Dictionary<int, IEnumerable<WixFilterEngineFootnote>> footnotesAllFilterEngine = new Dictionary<int, IEnumerable<WixFilterEngineFootnote>>();
            Dictionary<int,WixFootnote> allFootnotes = new Dictionary<int,WixFootnote>();

            string toReturn = String.Empty;
            string aboveTableReturn = String.Empty;
            string engineName = String.Empty;

            // wyciągam rok produkcji silnika
            WixEngine singleEngine = wixEngineDAO.FindByPK(engineId);
            if (singleEngine != null)
            {
                engineName = singleEngine.engine_name;
            }

            StringBuilder sbLeft = new StringBuilder();
            StringBuilder sbRight = new StringBuilder();
            sbLeft.Append("<div class='col-lg-6 col-md-12 col-sm-12  col-xs-12 no-padding mar-top-20'>");
            sbLeft.Append(generateTableHeaderLeftPart(languageName));

            sbRight.Append("<div class='col-lg-6 col-md-12 col-sm-12  col-xs-12 no-padding mar-top-20'>");
            sbRight.Append(generateTableHeaderRightPart(languageName));

            Dictionary<string,List<WixFilterEngine>> wixFilterOnlyDateIntervals = new Dictionary<string,List<WixFilterEngine>>();
            Dictionary<string, Dictionary<string, List<WixFilterEngine>>> wixFilterDateAndInfo = new Dictionary<string, Dictionary<string, List<WixFilterEngine>>>();
            Dictionary<string, List<WixFilterEngine>> wixFilterOnlyInfo = new Dictionary<string, List<WixFilterEngine>>();
            List<WixFilterEngine> wixFilterNoDateAndInfo = new List<WixFilterEngine>();



            
            IQueryable<WixFilterEngine> wixAllFilterEngineCollection =
                wixFilterEngine.FindByEngineAndModel(engineId, modelId);
            if (wixAllFilterEngineCollection != null)
            {
                foreach (WixFilterEngine singleWixFilterEngine in wixAllFilterEngineCollection)
                {
                    // wyciągam footnote
                    IQueryable<WixFilterEngineFootnote> wixFilterEngineFootnoteCollection = wixFilterEngineFootnoteDAO.FindByFilterEngineId(singleWixFilterEngine.filter_engine_id);
                    if (wixFilterEngineFootnoteCollection != null)
                    {
                        // robię kolekcję footnote związanego z konkretnym filter-engine
                        footnotesAllFilterEngine.Add(singleWixFilterEngine.filter_engine_id, wixFilterEngineFootnoteCollection.ToList());

                        // robię kolekcję unikatowych footnote wg footnote_id
                        foreach (WixFilterEngineFootnote filterEngineFootnote in wixFilterEngineFootnoteCollection)
                        {
                            if (!(allFootnotes.ContainsKey(filterEngineFootnote.footnote_id)))
                            {
                                int footnote_id = filterEngineFootnote.footnote_id;
                                WixFootnote wixFootnote = wixFootnoteDAO.FindByPK(footnote_id);
                                allFootnotes.Add(footnote_id, wixFootnote);
                            }
                        }
                    }

                    // tylko data
                    int[] wixFilterEngineCommentsOnlyDateCollection =
                        wixFilterEngineCommentsDAO.FindByFilterEngineIdOnlyDate(singleWixFilterEngine.filter_engine_id);
                    if (wixFilterEngineCommentsOnlyDateCollection != null)
                    {
                        string year_from =  Convert.ToString(wixFilterEngineCommentsOnlyDateCollection[0]);
                        string year_to = Convert.ToString(wixFilterEngineCommentsOnlyDateCollection[1]);
                        string year_from_to = year_from +";"+ year_to;

                        if (!(wixFilterOnlyDateIntervals.ContainsKey(year_from_to)))
                        {
                            wixFilterOnlyDateIntervals.Add(year_from_to,new List<WixFilterEngine>());
                        }

                        wixFilterOnlyDateIntervals[year_from_to].Add(singleWixFilterEngine);

                    }

                    // data + info
                    Dictionary<string,int[]> wixFilterEngineCommentsDateAndInfoCollection =
                        wixFilterEngineCommentsDAO.FindByFilterEngineIdDescAndYear(singleWixFilterEngine.filter_engine_id);
                    if (wixFilterEngineCommentsDateAndInfoCollection != null)
                    {
                        List<string> wix_filter_info = wixFilterEngineCommentsDateAndInfoCollection.Keys.ToList();
                        foreach(string info in wix_filter_info)
                        {
                            string year_from = Convert.ToString(wixFilterEngineCommentsDateAndInfoCollection[info][0]);
                            string year_to = Convert.ToString(wixFilterEngineCommentsDateAndInfoCollection[info][1]);
                            string year_from_to = year_from + ";" + year_to;

                            if (!(wixFilterDateAndInfo.ContainsKey(year_from_to)))
                            {
                                wixFilterDateAndInfo.Add(year_from_to, new Dictionary<string, List<WixFilterEngine>>());
                            }

                            if (!(wixFilterDateAndInfo[year_from_to].ContainsKey(info)))
                            {
                                wixFilterDateAndInfo[year_from_to].Add(info, new List<WixFilterEngine>());
                            }

                            wixFilterDateAndInfo[year_from_to][info].Add(singleWixFilterEngine);
                        }
                    }

                    // tylko info
                    IQueryable<WixFilterEngineComment> wixFilterEngineCommentsOnlyInfo =
                        wixFilterEngineCommentsDAO.FindByFilterEngineIdOnlyDescription(singleWixFilterEngine.filter_engine_id);
                    if (wixFilterEngineCommentsOnlyInfo != null)
                    {
                        foreach (WixFilterEngineComment info in wixFilterEngineCommentsOnlyInfo)
                        {
                            if(!(wixFilterOnlyInfo.ContainsKey(info.filter_additional_info)))
                                wixFilterOnlyInfo.Add(info.filter_additional_info, new List<WixFilterEngine>());

                            wixFilterOnlyInfo[info.filter_additional_info].Add(singleWixFilterEngine);
                        }
                    }
                }
            }

            IQueryable<WixFilterEngine> wixFiltersNulls = wixFilterEngine.FindByEngineAndModelOnlyNulls(engineId, modelId);
            if (wixFiltersNulls != null)
            {
                foreach (WixFilterEngine singleFilterEngine in wixFiltersNulls)
                    wixFilterNoDateAndInfo.Add(singleFilterEngine);
            }
            List<string> sortedIntervals = wixFilterOnlyDateIntervals.Keys.ToList();
            sortedIntervals.Sort();

            foreach (string interval in sortedIntervals)
            {
                List<WixFilterEngine> filterEngineToPresent = wixFilterOnlyDateIntervals[interval];
                //tylko data
                List<WixFilter> oilFiltersOnlyDate = new List<WixFilter>();
                List<WixFilter> fuelFiltersOnlyDate = new List<WixFilter>();
                List<WixFilter> airFiltersOnlyDate = new List<WixFilter>();
                List<WixFilter> cabinPaperFiltersOnlyDate = new List<WixFilter>();
                List<WixFilter> cabinCarbonFiltersOnlyDate = new List<WixFilter>();
                List<WixFilter> otherFiltersOnlyDate = new List<WixFilter>();

                string yearIntervalsArray = createStringFromInterval(interval);

                foreach (WixFilterEngine singleFilterEngine in filterEngineToPresent)
                {
                    string footnotesIndexString = getFootnotesIndexesForFilterEngingeId(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id);
                    WixFilter singleFilter = wixFilterDAO.FindByPK(singleFilterEngine.filter_id);
                    if (!singleFilter.filter_number.Contains(footnotesIndexString))
                    {
                        singleFilter.filter_number = singleFilter.filter_number + footnotesIndexString;
                    }   //if
                    if (singleFilter.filter_type_id.Equals(40))
                    {                        
                        oilFiltersOnlyDate.Add(singleFilter);
                    } // if oilFilters

                    else if (singleFilter.filter_type_id.Equals(47))
                    {
                        fuelFiltersOnlyDate.Add(singleFilter);

                    } // if fuelFilters

                    else if (singleFilter.filter_type_id.Equals(48))
                    {
                        airFiltersOnlyDate.Add(singleFilter);

                    } // if airFilters

                    else if (singleFilter.filter_type_id.Equals(52))
                    {
                        if (isFilterCabinCarbon(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id))
                        {
                            cabinCarbonFiltersOnlyDate.Add(singleFilter);
                        }   //if
                        else
                        {
                            cabinPaperFiltersOnlyDate.Add(singleFilter);
                        }   //else
                    } // if cabinFilter 

                    else
                    {
                        switch (singleFilter.filter_id)
                        {
                            case 5789:
                            case 5788:
                            case 5790:
                            case 5787:
                                break;
                            default:
                                otherFiltersOnlyDate.Add(singleFilter);
                                break;
                        }
                    } // if otherFilters
                } // foreach filter-engine
                if ((airFiltersOnlyDate.Count > 0) || (oilFiltersOnlyDate.Count > 0) ||
                   (fuelFiltersOnlyDate.Count > 0) || (cabinPaperFiltersOnlyDate.Count > 0) ||
                   (otherFiltersOnlyDate.Count > 0))
                {
                    sbLeft.Append(FiltersTableHelperWIX.generateFilterLeftPart(airFiltersOnlyDate, oilFiltersOnlyDate, yearIntervalsArray, String.Empty, airCol));
                    sbRight.Append(FiltersTableHelperWIX.generateFilterRightPart(fuelFiltersOnlyDate, cabinCarbonFiltersOnlyDate, cabinCarbonFiltersOnlyDate, otherFiltersOnlyDate));
                } //if
            } // foreach interval only with date


            // wyświetlenie filtrów z datą i info
            sortedIntervals = wixFilterDateAndInfo.Keys.ToList();
            sortedIntervals.Sort();


            foreach (string interval in sortedIntervals) 
            {
                Dictionary<string, List<WixFilterEngine>> infoDict = wixFilterDateAndInfo[interval];
                List<string> infoList = infoDict.Keys.ToList();
                infoList.Sort();
                string yearIntervalsArray = createStringFromInterval(interval);

                foreach (string info in infoList)
                {
                    //data plus info
                    List<WixFilter> oilFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilter> fuelFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilter> airFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilter> cabinPaperFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilter> cabinCarbonFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilter> otherFiltersDateAndDesc = new List<WixFilter>();
                    List<WixFilterEngine> filterEngineToPresent = infoDict[info];

                    foreach (WixFilterEngine singleFilterEngine in filterEngineToPresent)
                    {
                        string footnotesIndexString = getFootnotesIndexesForFilterEngingeId(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id);
                        WixFilter singleFilter = wixFilterDAO.FindByPK(singleFilterEngine.filter_id);
                        if (!singleFilter.filter_number.Contains(footnotesIndexString))
                        {
                            singleFilter.filter_number = singleFilter.filter_number + footnotesIndexString;
                        }   //if
                        if (singleFilter.filter_type_id.Equals(40))
                        {                            
                            oilFiltersDateAndDesc.Add(singleFilter);
                        } // if oilFilters

                        else if (singleFilter.filter_type_id.Equals(47))
                        {
                            fuelFiltersDateAndDesc.Add(singleFilter);

                        } // if fuelFilters

                        else if (singleFilter.filter_type_id.Equals(48))
                        {
                            airFiltersDateAndDesc.Add(singleFilter);

                        } // if airFilters

                        else if (singleFilter.filter_type_id.Equals(52))
                        {
                            if (isFilterCabinCarbon(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id)) {
                                cabinCarbonFiltersDateAndDesc.Add(singleFilter);
                            }
                            else
                            {
                                cabinPaperFiltersDateAndDesc.Add(singleFilter);
                            }
                        } // if cabinFilter 

                        else
                        {
                            switch (singleFilter.filter_id)
                            {
                                case 5789:
                                case 5788:
                                case 5790:
                                case 5787:
                                    break;
                                default:
                                    otherFiltersDateAndDesc.Add(singleFilter);
                                    break;
                            }
                        } // if otherFilters
                    } // foreach filter-engine
                    if ((airFiltersDateAndDesc.Count > 0) || (oilFiltersDateAndDesc.Count > 0) ||
                       (fuelFiltersDateAndDesc.Count > 0) || (cabinPaperFiltersDateAndDesc.Count > 0) ||
                       (otherFiltersDateAndDesc.Count > 0))
                    {
                        sbLeft.Append(FiltersTableHelperWIX.generateFilterLeftPart(airFiltersDateAndDesc, oilFiltersDateAndDesc, yearIntervalsArray, info.TrimEnd(' ', ';'), airCol));
                        sbRight.Append(FiltersTableHelperWIX.generateFilterRightPart(fuelFiltersDateAndDesc, cabinCarbonFiltersDateAndDesc, cabinPaperFiltersDateAndDesc, otherFiltersDateAndDesc));
                    } //if
                }
            } // foreach interval with date and info

            List<string> onlyInfo =  wixFilterOnlyInfo.Keys.ToList();
            onlyInfo.Sort();
            foreach (string info in onlyInfo)
            {
                //tylko opis
                List<WixFilter> oilFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilter> fuelFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilter> airFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilter> cabinPaperFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilter> cabinCarbonFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilter> otherFiltersOnlyDesc = new List<WixFilter>();
                List<WixFilterEngine> filterEngineToPresent = wixFilterOnlyInfo[info];

                foreach (WixFilterEngine singleFilterEngine in filterEngineToPresent)
                {
                    string footnotesIndexString = getFootnotesIndexesForFilterEngingeId(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id);
                    WixFilter singleFilter = wixFilterDAO.FindByPK(singleFilterEngine.filter_id);
                    if (!singleFilter.filter_number.Contains(footnotesIndexString))
                    {
                        singleFilter.filter_number = singleFilter.filter_number + footnotesIndexString;
                    }   //if
                    if (singleFilter.filter_type_id.Equals(40))
                    {
                        oilFiltersOnlyDesc.Add(singleFilter);
                    } // if oilFilters
                    else if (singleFilter.filter_type_id.Equals(47))
                    {
                        fuelFiltersOnlyDesc.Add(singleFilter);
                    } // if fuelFilters
                    else if (singleFilter.filter_type_id.Equals(48))
                    {
                        airFiltersOnlyDesc.Add(singleFilter);
                    } // if airFilters
                    else if (singleFilter.filter_type_id.Equals(52))
                    {
                        if (isFilterCabinCarbon(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id)) 
                        {
                            cabinCarbonFiltersOnlyDesc.Add(singleFilter);
                        }   //if
                        else
                        {
                            cabinPaperFiltersOnlyDesc.Add(singleFilter);
                        }   //else
                    } // if cabinFilter 
                    else
                    {
                        switch (singleFilter.filter_id)
                        {
                            case 5789:
                            case 5788:
                            case 5790:
                            case 5787:
                                break;
                            default:
                                otherFiltersOnlyDesc.Add(singleFilter);
                                break;
                        }
                    } // if otherFilters
                } // foreach filter-engine
                if ((oilFiltersOnlyDesc.Count > 0) || (airFiltersOnlyDesc.Count > 0) ||
                   (fuelFiltersOnlyDesc.Count > 0) || (cabinPaperFiltersOnlyDesc.Count > 0) ||
                   (otherFiltersOnlyDesc.Count > 0))
                {
                    sbLeft.Append(FiltersTableHelperWIX.generateFilterLeftPart(airFiltersOnlyDesc, oilFiltersOnlyDesc, String.Empty, info.TrimEnd(' ', ';'), airCol));
                    sbRight.Append(FiltersTableHelperWIX.generateFilterRightPart(fuelFiltersOnlyDesc, cabinCarbonFiltersOnlyDesc, cabinPaperFiltersOnlyDesc, otherFiltersOnlyDesc));
                } //if
            } // foreach only with info


            foreach (WixFilterEngine singleFilterEngine in wixFilterNoDateAndInfo)
            {
                // bez grupy
                List<WixFilter> oilFiltersNoGroup = new List<WixFilter>();
                List<WixFilter> fuelFiltersNoGroup = new List<WixFilter>();
                List<WixFilter> airFiltersNoGroup = new List<WixFilter>();
                List<WixFilter> cabinPaperFiltersNoGroup = new List<WixFilter>();
                List<WixFilter> cabinCarbonFiltersNoGroup = new List<WixFilter>();
                List<WixFilter> otherFiltersNoGroup = new List<WixFilter>();
                string footnotesIndexString = getFootnotesIndexesForFilterEngingeId(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id);
                WixFilter singleFilter = wixFilterDAO.FindByPK(singleFilterEngine.filter_id);
                if (!singleFilter.filter_number.Contains(footnotesIndexString))
                {
                    singleFilter.filter_number = singleFilter.filter_number + footnotesIndexString;
                }   //if
                if (singleFilter.filter_type_id.Equals(40))
                {
                    oilFiltersNoGroup.Add(singleFilter);
                } // if oilFilters

                else if (singleFilter.filter_type_id.Equals(47))
                {
                    fuelFiltersNoGroup.Add(singleFilter);
                } // if fuelFilters

                else if (singleFilter.filter_type_id.Equals(48))
                {
                    airFiltersNoGroup.Add(singleFilter);
                } // if airFilters

                else if (singleFilter.filter_type_id.Equals(52))
                {
                    if (isFilterCabinCarbon(footnotesAllFilterEngine, allFootnotes, singleFilterEngine.filter_engine_id))
                    {
                        cabinCarbonFiltersNoGroup.Add(singleFilter);
                    }
                    else
                    {
                        cabinPaperFiltersNoGroup.Add(singleFilter);
                    }
                } // if cabinFilter 
                else
                {
                    switch (singleFilter.filter_id)
                    {
                        case 5789:
                        case 5788:
                        case 5790:
                        case 5787:
                            break;
                        default:
                            otherFiltersNoGroup.Add(singleFilter);
                            break;
                    }
                } // if otherFilters
                if ((airFiltersNoGroup.Count > 0) || (oilFiltersNoGroup.Count > 0) ||
                   (fuelFiltersNoGroup.Count > 0) || (cabinPaperFiltersNoGroup.Count > 0) ||
                   (otherFiltersNoGroup.Count > 0))
                {
                    sbLeft.Append(FiltersTableHelperWIX.generateFilterLeftPart(airFiltersNoGroup, oilFiltersNoGroup, String.Empty, String.Empty, airCol));
                    sbRight.Append(FiltersTableHelperWIX.generateFilterRightPart(fuelFiltersNoGroup, cabinCarbonFiltersNoGroup, cabinPaperFiltersNoGroup, otherFiltersNoGroup));
                } //if
            } // foreach with no group

            sbLeft.Append("</div>");
            sbRight.Append("</div>");
            StringBuilder sb = new StringBuilder();
            sb.Append(sbLeft.ToString());
            sb.Append(sbRight.ToString());
            sb.Append(FiltersTableHelperWIX.generateFootnoteDescriptionList(allFootnotes));
            return sb.ToString();
        } // end generateFiltersTable

        private static string createStringFromInterval(string wixFilterEngine)
        {
            string toReturn = "";
 	        // uzupełnienie rocznika

            string[] years = wixFilterEngine.Split(';');
            if (years[0] == years[1])
                toReturn = years[0];
            else
                toReturn = years[0] + "-" + years[1];

            return toReturn;
        }
        private static bool hasFilterEngineAdditionalInfo(WixFilterEngine singleFilterEngine)
        {
            string info = singleFilterEngine.filter_additional_info;
            if ((info != null) && (info.Trim().Length > 0))
                return true;
            return false;
        }

        private static bool isFilterCabinCarbon(Dictionary<int, IEnumerable<WixFilterEngineFootnote>> footnotesAllFilterEngine, Dictionary<int, WixFootnote> allFootnotes,int singleFilterEngineId)
        {
            if (!footnotesAllFilterEngine.ContainsKey(singleFilterEngineId))
                return false;

            foreach (WixFilterEngineFootnote footnote in footnotesAllFilterEngine[singleFilterEngineId]) {
                if (allFootnotes[footnote.footnote_id].org_footnote_id == 78)
                    return true;
            }
            return false;
        }

        private static string getFootnotesIndexesForFilterEngingeId(Dictionary<int, IEnumerable<WixFilterEngineFootnote>> footnotes, Dictionary<int, WixFootnote> allFootnotes, int filterEngineId)
        {
            string toReturn = "";
            if (footnotes.ContainsKey(filterEngineId))
            {
                toReturn += " <sup style=\"vertical-align:top; font-size: 8px;\">";
                foreach (WixFilterEngineFootnote filterEngineFootnote in footnotes[filterEngineId])
                {
                    toReturn += String.Format("{0}) ", allFootnotes[filterEngineFootnote.footnote_id].org_footnote_id);
                }
                toReturn += "</sup>";
            }

            return toReturn;
        }

        private static string generateFootnoteDescriptionList(Dictionary<int, WixFootnote> allFootnotes)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<int, string> footnotes = new Dictionary<int, string>();
            foreach (int footnoteId in allFootnotes.Keys)
            {
                WixFootnote oneFootnote = allFootnotes[footnoteId];
                footnotes.Add(oneFootnote.org_footnote_id, oneFootnote.footnote_name);
            }
            List<int> sortedFootnoteIds = footnotes.Keys.ToList();
            sortedFootnoteIds.Sort();
            foreach (int footnoteOrgId in sortedFootnoteIds)
            {
                if (footnoteOrgId != 72)
                {
                    sb.Append("<div class='col-xs-12 no-padding'>");
                    sb.Append(String.Format("<p style=\" float: left; margin-bottom: 4px; vertical-align: top; font-size: 8px; font-family: 'Open Sans',sans-serif; font-weight: 300; color: #000; \">{0})</p>", footnoteOrgId));
                    sb.Append(String.Format("<p style=\" float: left; margin-bottom: 4px; padding-left: 5px; font-size: 11px; font-family: 'Open Sans',sans-serif; font-weight: 300; color: #000; \">{0}</p>", footnotes[footnoteOrgId]));
                    sb.Append("</div>");
                }   //if
            }   //for
            return sb.ToString();
        }

        private static int getToLastDigits(int year)
        {
            if (year.Equals(0))
            {
                return 0;
            }
            string lastTwoDigitsHelper = Convert.ToString(year);
            return Convert.ToInt32(lastTwoDigitsHelper.Substring(2, 2));
        }

        private static int addTwoDigitsToYear(string year)
        {
            if (year == "")
            {
                return 0;
            }
            int intYear = Convert.ToInt32(year);
            if (intYear < 30)
            {
                return 20;
            }
            else
            {
                return 19;
            }
        }

        private static string generateTableHeaderLeftPart(string languageName)
        {
            int airCol = 6;
            int airCol2 = 6;
            if (languageName == "polish")
            {
                airCol = 7;
                airCol2 = 5;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-xs-12 no-padding'>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding overlay-engine2 '>");
            sb.Append("<div class='col-xs-5 no-padding '>");
            sb.Append(String.Format("<img class='img-responsive sand-clock' src='{0}' alt='cc'>", VirtualPathUtility.ToAbsolute("~/Content/img/sand-clock.png")));
            sb.Append("</div>");
            sb.Append("<div class='col-xs-7 no-padding overlay-engine-boder-r overlay-engine-boder-l'>");
            sb.Append(String.Format("<p>{0}</p>", translationHelper.getTranslation("_hlpDescription", languageName)));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding no-padding overlay-engine2'>");
            sb.Append(String.Format("<div class='col-xs-{0} no-padding overlay-engine-boder-r'>", airCol.ToString()));
            sb.Append(String.Format("<p>{0}</p>", translationHelper.getTranslation("_airFilter", languageName)));
            sb.Append(String.Format("<img class='img-responsive' src='{0}' alt='cc'>", VirtualPathUtility.ToAbsolute("~/Content/img/car-fan.png")));
            sb.Append("</div>");
            sb.Append(String.Format("<div class='col-xs-{0} no-padding overlay-engine-boder-r'>", airCol2.ToString()));
            sb.Append(String.Format("<p>{0}</p>", translationHelper.getTranslation("_oilFilter", languageName)));
            sb.Append(String.Format("<img class='img-responsive' src='{0}' alt='cc' style='margin-top: 5px;'>", VirtualPathUtility.ToAbsolute("~/Content/img/oil-drop.png")));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }

        private static string generateTableHeaderRightPart(string languageName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-xs-12 no-padding'>");
            sb.Append("<div class='col-sm-3 col-xs-3 no-padding overlay-engine2 overlay-engine-boder-r'>");
            sb.Append("<p>Fuel</p>");
            sb.Append(String.Format("<img class='img-responsive' src='{0}' alt='cc'>", VirtualPathUtility.ToAbsolute("~/Content/img/tank-ikon.png")));
            sb.Append("</div>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding overlay-engine2 overlay-engine-boder-r'>");
            sb.Append(String.Format("<p class='cabin'>{0}</p>", translationHelper.getTranslation("_cabinFilter", languageName)));
            sb.Append("<div class='col-xs-6  no-padding'>");
            sb.Append(String.Format("<p class='cabin-paper'>{0}</p>", translationHelper.getTranslation("_cabinPaper", languageName)));
            sb.Append("</div>");
            sb.Append("<div class='col-xs-6  no-padding'>");
            sb.Append(String.Format("<p class='cabin-carbon'>{0}</p>", translationHelper.getTranslation("_cabinCarbon", languageName)));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<div class='col-sm-3 col-xs-3 no-padding overlay-engine2 '>");
            sb.Append(String.Format("<p>{0}</p>", translationHelper.getTranslation("_hlpOther", languageName)));
            sb.Append("<p>Other</p>");
            sb.Append("</div>");
            sb.Append("</div>");
            return sb.ToString();
        }
        private static string generateFilterLeftPart(List<WixFilter> airFiltersArray, List<WixFilter> oilFiltersArray, string yearIntervalsArray, string description)
        {
            return FiltersTableHelperWIX.generateFilterLeftPart(airFiltersArray, oilFiltersArray, yearIntervalsArray, description, 6);
        }
        private static string generateFilterLeftPart(List<WixFilter> airFiltersArray, List<WixFilter> oilFiltersArray, string yearIntervalsArray, string description, int airCol)
        {
            if ((airFiltersArray == null) || (oilFiltersArray == null))
            {
                return String.Empty;                    
            }   //if
            if (!String.IsNullOrEmpty(description))
            {
                description = description.Replace(";", String.Empty).Trim();
            }   //if
            int airCol2 = 12 - airCol;
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-xs-12 no-padding filter-border-l-r'>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding filter-nfo filter-nfo-center-parent '>");
            sb.Append("<div class='col-xs-5 no-padding filter-nfo-center-parent filter-border-r'>");
            sb.Append(String.Format("<p class='filter-nfo-light  filter-nfo-center-child'>{0}</p>", yearIntervalsArray));
            sb.Append("</div>");
            sb.Append("<div class='col-xs-7 no-padding filter-nfo-center-parent filter-border-r '>");
            sb.Append(String.Format("<p  class='filter-nfo-light filter-nfo-center-child' style='top:25%;'>{0}</p>", description));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding no-padding filter-nfo '>");
            sb.Append(String.Format("<div class='col-xs-{0} no-padding filter-nfo-center-parent filter-border-r filter-hover'>", airCol.ToString()));
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(airFiltersArray)));
            sb.Append("</div>");
            sb.Append(String.Format("<div class='col-xs-{0} no-padding filter-nfo-center-parent filter-hover'>", airCol2.ToString()));
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(oilFiltersArray)));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("</div>");         
            return sb.ToString();
        }

        private static string generateFilterRightPart(List<WixFilter> fuelFiltersArray, List<WixFilter> cabinPaperFiltersArray, List<WixFilter> cabinCarbonFiltersArray, List<WixFilter> otherFiltersArray)
        {
            if ((fuelFiltersArray == null) || (cabinPaperFiltersArray == null) || (cabinCarbonFiltersArray == null) || (otherFiltersArray == null))
            {
                return String.Empty;
            }   //if
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class='col-xs-12 no-padding  filter-border-l-r'>");
            sb.Append("<div class='col-sm-3 col-xs-3 no-padding filter-nfo-center-parent filter-border-r filter-hover'>");
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(fuelFiltersArray)));
            sb.Append("</div>");
            sb.Append("<div class='col-sm-6 col-xs-6 no-padding  filter-border-r'>");
            sb.Append("<div class='col-xs-6 filter-nfo-center-parent no-padding filter-hover'>");
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(cabinCarbonFiltersArray)));
            sb.Append("</div>");
            sb.Append("<div class='col-xs-6 filter-nfo-center-parent no-padding filter-hover'>");
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(cabinPaperFiltersArray)));
            sb.Append("</div>");
            sb.Append("</div>");
            sb.Append("<div class='col-sm-3 col-xs-3 no-padding filter-nfo-center-parent filter-hover'>");
            sb.Append(String.Format("<p  class='filter-nfo-bold filter-nfo-center-child'>{0}</p>", FiltersTableHelperWIX.buildFilterNumbersFromList(otherFiltersArray)));
            sb.Append("</div>");
            sb.Append("</div>");

            return sb.ToString();
        }

        private static string buildFilterNumbersFromList(List<WixFilter> filters)
        {
            string result = String.Empty;
            if (filters == null)
            {
                return result;
            }   //if        
            int counter = 0;
            foreach (WixFilter filter in filters)
            {
                if (counter > 0)
                {
                    result += "<br/>";
                }
                result += String.Format("<span style='cursor: pointer' class='filter-selector' data-filter='{0}'>{1}</span>", filter.filter_id, filter.filter_number);
                counter++;
            }   //for
            return result;
        }
    }
}
